using DG.Tweening;
using UnityEngine;

public class RingMesh : MeshBase
{
    public int segment = 1;
    public float ringWidth;
    public float m_RngRadius;
    public float m_AngleBegin;
    public float m_AngleEnd;

    protected override void InitMesh()
    {
        if (mMeshFilter == null) Init();
        segment = segment > 0 ? segment : 1;
        mTriangles=new int[segment*2*3];     
        for (int i = 0; i < segment; i++)
        {
            int k = i*6;
            int j = i*2;
            mTriangles[k] = j;  
            mTriangles[k + 1] = j + 2;
            mTriangles[k + 2] = j + 3;
            mTriangles[k + 3] = j;
            mTriangles[k + 4] = j + 3;
            mTriangles[k + 5] = j + 1;
        }
        int vertexCount = segment*2 + 2;
        mUVs=new Vector2[vertexCount];
        float singleUV = 1f/segment;
        float uvY = 0f;
        for (int i = 0; i < vertexCount; i+=2)
        {
            mUVs[i].x = 0f;
            mUVs[i + 1].x = 1f;
            mUVs[i].y = uvY;
            mUVs[i + 1].y = uvY;
            uvY += singleUV;
        }     
        UpdateShape();
    }
    
    protected override void UpdateShape()
    {
        int vertexCount = segment*2 + 2;
        mVector3s=new Vector3[vertexCount];

        float angle = m_AngleBegin*Mathf.Deg2Rad;
        float wHalf = ringWidth*0.5f;
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);

        float minRingRadius = m_RngRadius - wHalf;
        float maxRingRadius = m_RngRadius + wHalf;

        float x = cos*minRingRadius;
        float y = 0;
        float z = sin*minRingRadius;

        mVector3s[0].x = x;
        mVector3s[0].y = y;
        mVector3s[0].z = z;

        x = cos*maxRingRadius;
        z = sin*maxRingRadius;

        mVector3s[1].x = x;
        mVector3s[1].y = y;
        mVector3s[1].z = z;

        float singleAngle = (m_AngleEnd - m_AngleBegin)/segment*Mathf.Deg2Rad;
        for (int i = 0; i < segment; i++)
        {
            angle += singleAngle;
            sin = Mathf.Sin(angle);
            cos = Mathf.Cos(angle);
            x = cos*minRingRadius;
            y = 0;
            z = sin*minRingRadius;
            mVector3s[i*2+2]=new Vector3(x,y,z);

            x = cos*maxRingRadius;
            y = 0;
            z = sin*maxRingRadius;
            mVector3s[i*2+3]=new Vector3(x,y,z);
        }
        UpdateMesh();
    }


    private float AngleBegin
    {
        get => m_AngleBegin;
        set { m_AngleBegin = value; UpdateShape(); }
    }
    
    public float AngleEnd
    {
        get => m_AngleEnd;
        set { m_AngleEnd = value; UpdateShape(); }
    }

    public override Tweener DoAnim(float endValue, float duration, float delay)
    {
        return DOTween.To(() => AngleEnd, x => AngleEnd = x, endValue, duration).SetDelay(delay);
    }

    public void DoRotate(float duration, float delay)
    {
        transform.DOLocalRotate(Vector3.up * 360, duration, RotateMode.LocalAxisAdd).SetDelay(delay).SetLoops(-1);
    }
}
